<template>
  <div>
    <div class="search">
      <input
        type="text"
        class="search__input"
        @keydown="isMenuShow = true"
        @focus="isMenuShow = true"
        @blur="isMenuShow = false"
        ref="inputRef"
        :placeholder="placeholder"
        v-model="inputValue"
      />
      <div class="search__icon">
        <span>
          <icon-font type="icon-sousuotianchong" />
        </span>
      </div>
      <transition name="menu">
        <div class="select_menu" ref="menuRef" v-show="isMenuShow">
          <div
            class="menu_item"
            v-for="(item, index) of menudata.slice(0, 4)"
            :key="index"
            @click="getMenuItem(item)"
          >{{ item.label }}</div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script lang="ts" setup>
import TMenu from './TMenu.vue'
import { reactive, ref, onMounted, watch, computed } from 'vue'
import { IconFont } from '@/base-ui/Icons/index'
import { IInputSize, IInputDataItem } from '../type'

const menuRef = ref<HTMLElement | null>(null)
const inputRef = ref<HTMLElement | null>(null)
const isMenuShow = ref(false)
const props = withDefaults(
  defineProps<{
    size?: string
    data?: any
    placeholder?: string
  }>(),
  {
    size: "mini",
    placeholder: 'Search...',
    data: [
      // {
      //   id: 1,
      //   value: 'test1',
      //   label: 'TEST1',
      // },
      //   {
      //   id: 2,
      //   value: 'shall we talk',
      //   label: 'Shall we Talk'
      // },
      //   {
      //   id: 3,
      //   value: '1874',
      //   label: '1874'
      // },
      //   {
      //   id: 4,
      //   value: 'eason',
      //   label: 'EASON'
      // }
    ]
  }
)
const emit = defineEmits(['update:modelValue'])

const menudata = ref([] as any)
onMounted(() => {
  menudata.value = props.data
})

const inputValue = ref('')
computed(() => {
const getMenuItem = (item: any) => {
  inputValue.value = item.value
}

watch(inputValue, (newVal) => {
  emit('update:modelValue', newVal)
  filterData(newVal)
})

const filterData = (value: any) => {
  menudata.value = props.data.filter((item: { label: string }) => {
    return item.label.toLowerCase().includes(inputValue.value.toLocaleLowerCase())
  })
}


const placeholder = props.placeholder
const setSizeStyle = (size?: string) => {
  const miniStyle = {
    width: size === 'mini' ? '150px' : '250px',
    height: size === 'mini' ? '35px' : '40px',
    fontSize: size === 'mini' ? '13px' : '22px'
  }
  return miniStyle
}
const styleData = setSizeStyle(props.size)

const { height: th, width: tw, fontSize: fs } = styleData

</script>
<style lang="less" scoped>
/*  SEARCH  */
.search {
  // width: 150px;
  // height: 35px;
  height: v-bind(th);
  display: flex;
  align-items: center;
  margin-left: 500px;
  position: relative;
}
.search__input {
  margin-top: 50px;
  margin-left: -10px;
  width: v-bind(tw);
  height: v-bind(th);
  border: none;
  border-radius: 1rem;
  font-size: v-bind(fs);
  text-align: left;
  padding-left: 45px;
  box-shadow: inset 0.2rem 0.2rem 0.5rem @greyLight-2,
    inset -0.2rem -0.2rem 0.5rem @white;
  background: none;
  font-family: inherit;
  color: @greyDark;
}
.search__icon {
  margin: 45px 0 0 -15px;
  width: 20px;
}
.search__input::-moz-placeholder {
  color: @greyLight-3;
}
.search__input:-ms-input-placeholder {
  color: greyLight-3;
}
.search__input::placeholder {
  color: greyLight-3;
}
.search__input:focus {
  outline: none;
  box-shadow: 0.3rem 0.3rem 0.6rem @greyLight-2, -0.2rem -0.2rem 0.5rem @white;
}
.search__input:focus + .search__icon {
  color: @primary;
}
.search__icon {
  height: 2rem;
  position: absolute;
  font-size: 1.5rem;
  padding: 0 1rem;
  display: flex;
  color: @greyDark;
  transition: 0.3s ease;
}
// nemu
.select_menu {
  position: absolute;
  margin: 250px 0 0 -7px;
  // padding: 15px 50px 0 20px ;
  width: v-bind(tw);
  height: 150px;
  border-radius: 1rem;
  box-shadow: inset 0.2rem 0.2rem 0.5rem @greyLight-2,
    inset -0.2rem -0.2rem 0.5rem @white;
  background: none;
  transition: 1s;
}
.menu_item {
  margin-top: 10px;
  margin-left: 20px;
  height: 25px;
  font-size: v-bind(fs);
  font-weight: 400;
  // align-items: center;
  cursor: pointer;
  line-height: 20px;
  // box-shadow: inset 0.2rem 0.2rem 0.5rem @greyLight-2,
  // inset -0.2rem -0.2rem 0.5rem @white;
  background: none;
  transition: margin-left 0.5s;
  &:hover {
    margin-left: 0;
    width: v-bind(tw);
    outline: none;
    font-size: v-bind(fs);
    text-align: center;
    font-weight: 600;
    border-radius: 8%;
    // background-color: black;
    box-shadow: 0.3rem 0.3rem 0.6rem @greyLight-2, -0.2rem -0.2rem 0.5rem @white;
  }
}
.menu-enter-from,
.menu-leave-to {
  opacity: 0;
}
.menu-enter-to,
.menu-leave-from {
  opacity: 1;
}
.menu-enter-active,
.menu-leave-active {
  transition: opacity 1s ease;
}

.menu-enter-active {
  animation: bounce 1s ease;
}
.menu-leave-active {
  animation: bounce 1s ease reverse;
}

@keyframes bounce {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.3);
  }
  100% {
    transform: scale(1);
  }
}
</style>
